/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_profile.js
*  Description  : 프로필 등록/수정
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-27  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 프로필 사진
	function clsProfileImage()
	{
		this._HTMLDom = null;
		this.infoData = null;
		this.genderCode = null;
		
		this._init();
	}
	
	clsProfileImage.prototype = {
		_init: function(){
			var $layer = $("div.profile");
			var thisClass = this;
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.$getImage().width(135).height(135);
			$layer.find("span.btn_del").height(15);
			
			this.setInfo({
				"파일명": ""
				, "원본파일명": ""
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, setGenderCode: function(genderCode){
			this.genderCode = genderCode;
		}
		, getInfo: function(){
			return this.infoData;
		}
		, $getImage: function(){
			return this._HTMLDom["$layer"].find("img[alt='사진']");
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var of = this.infoData["원본파일명"];
			
			if(_util.nvl(this.infoData["파일명"]).indexOf("graph.facebook.com") != -1){
				of = "페이스북 프로필 이미지";
			}
			
			this.$getImage().attr("src", _util.nvl(_gf.filePath2Url(this.infoData["파일명"]), _gf.getDefaultProfileImage())).unbind("error").error(function(){
				var img = _gf.getDefaultProfileImage(thisClass.genderCode);
				
				$(this).attr("src", img);
			});
			
			var $tmp = $layer.find("div.profile_photo span.btn_del").children().clone(true)
			$layer.find("div.profile_photo span.btn_del").html(_util.nvl(of)).append($tmp);
			
		}
		, $getBtnAttach: function(){
			return this._HTMLDom["$layer"].find("img[alt='사진첨부']");
		}
		, setAttach: function(callback){
			var $img = this.$getLayer().find("img[alt='사진첨부']");
			var p = $img.position();
			
			_gf.setUploadFileImage(this.$getLayer().find("img[alt='사진첨부']"), null, function(ret){
				callback.apply(null, arguments);
			}, p);
		}
		, setDelete: function(callback){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.on("click", "img[alt='삭제']", function(){
				callback.apply(null, arguments);
			});
		}
	}
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		, profileImageSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "updateMyProfilePhoto";
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, {
				"fileSeq": arg["파일코드"]
				, "orgnFileName": arg["원본파일명"]
				, "saveFileName": arg["파일명"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishProfileImageSubmit(res);
			});
		}
	}
	
	
	function clsControl()
	{
		this.objComm = null;
		this.objProfileImage = null;
		
		this.openerControl = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
			if(!this.openerControl){
				self.close();
				return;
			}
			
			this.objComm = new clsComm();
			this.objProfileImage = new clsProfileImage();
			
			this._bind();
			this._start();
			
			_util.setWindowSize();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 사진첨부
			this.objProfileImage.setAttach(function(ret){
				if(ret["결과코드"] != "00000"){
					alert("파일업로드 오류발생!!");
					return;
				}
				
				var fs = ret["파일리스트"][0]["파일크기"]-0;
			
				if(fs >= 3 * 1024 * 1024){
					alert("사진용량은 3MB까지만 등록 가능합니다. 파일 용량을 확인하여 주세요.");
					return;
				}
				
				var i = thisClass.objProfileImage.getInfo();
				
				var fileList = ret["파일리스트"][0];
				
				fileList["파일코드"] = i["파일코드"];
				
				thisClass.objProfileImage.setInfo(fileList);
			});
			
			// 첨부삭제
			this.objProfileImage.setDelete(function(){
				var i = thisClass.objProfileImage.getInfo();
				
				thisClass.objProfileImage.setInfo({
					"파일코드": i["파일코드"]
				});
			});
			
			// 확인
			$("img[alt='확인']").click(function(){
				var infoData = thisClass.openerControl.getProfileImageData();
				var fileData = thisClass.objProfileImage.getInfo();
				
				if(infoData["파일명"] == fileData["파일명"]){
					self.close();
					return;
				}
				
				_gf.spinStart("sending...");
				
				thisClass.objComm.profileImageSubmit({
					"파일코드": _util.nvl(fileData["파일코드"])
					, "파일명": _util.nvl(fileData["파일명"])
					, "원본파일명": _util.nvl(fileData["원본파일명"])
				});
			});
			
			// 닫기
			$("img[alt='취소'], img[alt='close']").click(function(){
				self.close();
			});
		}
		, _start: function(){
			// 파일정보 세팅
			var infoData = this.openerControl.getProfileImageData();
			var genderCode = this.openerControl.getGenderCode();
			
			this.objProfileImage.setInfo(infoData);
			this.objProfileImage.setGenderCode(genderCode);
		}
		// 파일 업로드 완료
		, finishProfileImageSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			_gf.spinEnd();
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			this.openerControl.closeProfileImage(self);
			self.close();
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});